﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

using ECS.Framework;
using ECS.DAL;
using ECS.DAL.Component;
using ECS.DAL.Entity;

namespace E_ICS.E_ICS
{
    public partial class frmEditWeightEntry : Form
    {
        #region private properties

       // DataSet data = null;
        #endregion

        #region form event
        
        public frmEditWeightEntry()
        {
            InitializeComponent();
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                ClearValueEditWeight();
                this.Close();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (DataValidation())
                {
                    saveWeightEntry();
                    this.DialogResult = DialogResult.Yes;
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        private void frmEditWeightEntry_Load(object sender, EventArgs e)
        {
            SetScreen();
            try
            {
                Loadscreen();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
            
        }
        /// <summary>
        /// load result from DB
        /// </summary>
         private void SetScreen()
        {
            this.Text = CommonResourceString.GetResourseValue("scn_EditWeightEntry_frm");
            lblPallet.Text = CommonResourceString.GetResourseValue("scn_EditWeightEntry_label4");
            lblGrossDisplay.Text = CommonResourceString.GetResourseValue("scn_EditWeightEntry_label1");
            lblTareDisplay.Text = CommonResourceString.GetResourseValue("scn_EditWeightEntry_label2");
            lblNetDisplay.Text = CommonResourceString.GetResourseValue("scn_EditWeightEntry_label3");
            btnSave.Text = CommonResourceString.GetResourseValue("scn_EditWeightEntry_btnSave");
            btnCancel.Text = CommonResourceString.GetResourseValue("scn_EditWeightEntry_btnCancel");
        

                  
          
        }

        private void txtPallet_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
            {
                e.Handled = true;
            }
        }
        #endregion

        #region private methods
        /// <summary>
        /// validate data
        /// </summary>
        /// <returns></returns>
        private bool DataValidation()
        {
            if (string.IsNullOrEmpty(txtPallet.Text))
            {
                CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_EditWeightEntry_Msg_pallet"));
                //CommonMessageBox.ShowMessageWarning("Pallet count could not be empty.");
                return false;
            }
            return true;
        }
        
        private void Loadscreen()
        {
            txtPallet.Text = ViewEditWeightId.pallet_count;
            lblGross.Text = ViewEditWeightId.gross_weight;
            lblTare.Text = ViewEditWeightId.tare_weight;
            lblNet.Text = ViewEditWeightId.net_weight;
        }
        
        private void ClearValueEditWeight()
        {
            ViewEditWeightId.editwawb_Id = string.Empty;
            ViewEditWeightId.editweight_Id = string.Empty;
            ViewEditWeightId.editslip_Id = string.Empty;
            ViewEditWeightId.pallet_count = string.Empty;
            ViewEditWeightId.gross_weight = string.Empty;
            ViewEditWeightId.tare_weight = string.Empty;
            ViewEditWeightId.net_weight = string.Empty;
            ViewEditWeightId.weight_number = string.Empty;
        }

        /// <summary>
        /// save weight to DB 
        /// </summary>
        private void saveWeightEntry()
        {
            try
            {
                WeightRecord param = new WeightRecord();
                WeightRecordComponent compData = new WeightRecordComponent();
                param.AppId = GlobalVariable.user.ApplicationID;
                param.Device = GlobalVariable.user.Device;
                param.UserId = GlobalVariable.user.UserID;
                param.WeightId = ViewEditWeightId.editweight_Id;
                param.SlipId = ViewEditWeightId.editslip_Id;
                param.WeightNumber = Convert.ToInt32(ViewEditWeightId.weight_number);
                param.GrossWeight = Convert.ToSingle(ViewEditWeightId.gross_weight);
                param.TareWeight = Convert.ToSingle(ViewEditWeightId.tare_weight);
                param.NetWeight = Convert.ToSingle(ViewEditWeightId.net_weight);
                param.PalletCount = Convert.ToInt32(txtPallet.Text);
                compData.Update(param);
                //SqlCommand cmd = new SqlCommand();
                //cmd.CommandText = "usp_UpdateWeightRecord";
                //cmd.CommandType = CommandType.StoredProcedure;
                //cmd.Connection = sqlCon;
                //cmd.Parameters.AddWithValue("@in_vchApplicationID", appId);
                //cmd.Parameters.AddWithValue("@in_vchWeightID", ViewEditWeightId.editweight_Id);
                //cmd.Parameters.AddWithValue("@in_vchSlipID", ViewEditWeightId.editslip_Id);
                //cmd.Parameters.AddWithValue("@in_intWeightNumber", Convert.ToInt32(ViewEditWeightId.weight_number));
                //cmd.Parameters.AddWithValue("@in_fltGrossWeight", Convert.ToDouble(ViewEditWeightId.gross_weight));
                //cmd.Parameters.AddWithValue("@in_fltTareWeight", Convert.ToDouble(ViewEditWeightId.tare_weight));
                //cmd.Parameters.AddWithValue("@in_fltNetWeight", Convert.ToDouble(ViewEditWeightId.net_weight));
                //cmd.Parameters.AddWithValue("@in_intPalletCount", Convert.ToDouble(txtPallet.Text));
                //cmd.Parameters.AddWithValue("@in_vchDevice", device);
                //cmd.Parameters.AddWithValue("@in_vchUserID", GlobalVariable.userName);
                //sqlCon.Open();
                //SqlDataAdapter da = new SqlDataAdapter(cmd);
                //data = new DataSet();
                //da.Fill(data, "InsertDimensionRecord");
                //string status = data.Tables[0].Rows[0]["Status"].ToString();
                //string Messagest = data.Tables[0].Rows[0]["Message"].ToString();
                //if (status == "PASS")
                //{
                //    // MessageBox.Show("OK");
                //}
                //else
                //{
                //    MessageBox.Show(Messagest);
                //}
                //sqlCon.Close();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        #endregion
       
    }
}
